package com.qxsx.midi.activity.me;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.Base64Coder;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.GalleryImage;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Joney on 2015/9/9.
 * 图片申请
 */
public class ApplyImageActivity extends BaseActivity implements OnClickListener {

    private GalleryImage hl_listview;
    private List<String> list;

    private Button btn_apply; //申请

    //要上传的图片数据
    private LinearLayout ll_add1, ll_add2, ll_add3;  //添加图片的布局
    private ImageView iv_img1, iv_img2, iv_img3;  //已经添加的布局
    private EditText edt_one, edt_two, edt_three;
    private EditText edt_name, edt_desc;//文章标题，正文内容

    //要上传的图片数据
    private String[] photoStream = new String[]{"", "", ""};
    private static final int IMAGE_REQUEST_CODE = 0;
    private static final int RESULT_REQUEST_CODE = 2;
    private static int TAG = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_apply);
        setNavBtn(R.mipmap.back, 0);
        setTitle("图片申请");
        findById();
    }


    private void findById() {
        edt_name = (EditText) findViewById(R.id.edt_name);
        edt_desc = (EditText) findViewById(R.id.edt_desc);
        //图片描述
        edt_one = (EditText) findViewById(R.id.edt_one);
        edt_two = (EditText) findViewById(R.id.edt_two);
        edt_three = (EditText) findViewById(R.id.edt_three);
        //图片设置
        iv_img1 = (ImageView) findViewById(R.id.iv_img1);
        iv_img2 = (ImageView) findViewById(R.id.iv_img2);
        iv_img3 = (ImageView) findViewById(R.id.iv_img3);
        //点击添加
        ll_add1 = (LinearLayout) findViewById(R.id.ll_add1);
        ll_add1.setOnClickListener(this);
        ll_add2 = (LinearLayout) findViewById(R.id.ll_add2);
        ll_add2.setOnClickListener(this);
        ll_add3 = (LinearLayout) findViewById(R.id.ll_add3);
        ll_add3.setOnClickListener(this);


        btn_apply = (Button) findViewById(R.id.btn_apply);


        btn_apply.setOnClickListener(this);
    }


    private void applyimage() {

    }


    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ll_add1:
                TAG = 3;
                Intent intent = new Intent();
                intent.setType("image/*"); // 设置文件类型
                intent
                        .setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent,
                        IMAGE_REQUEST_CODE);
                break;
            case R.id.ll_add2:
                TAG = 4;
                Intent intent1 = new Intent();
                intent1.setType("image/*"); // 设置文件类型
                intent1.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent1,
                        IMAGE_REQUEST_CODE);
                break;
            case R.id.ll_add3:
                TAG = 5;
                Intent intent2 = new Intent();
                intent2.setType("image/*"); // 设置文件类型
                intent2.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent2,
                        IMAGE_REQUEST_CODE);

                break;
            case R.id.btn_apply:
                ToApply();
                break;

        }
    }

    /**
     * 图片申请
     */
    private void ToApply() {

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        try {
            object.put("user_id", user_id);
            object.put("title", edt_name.getText().toString() + "");
            object.put("content", edt_desc.getText().toString() + "");
            object.put("img1", photoStream[0].toString());
            object.put("remark1", edt_one.getText().toString() + "");
            object.put("img2", photoStream[1].toString());
            object.put("remark2", edt_two.getText().toString() + "");
            object.put("img3", photoStream[2].toString());
            object.put("remark3", edt_three.getText().toString() + "");
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("8041", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (array == null) {
                    Log.e("code", code.toString());
                    Log.e("message", message.toString());
                } else {
                    Log.e("code", code.toString());
                    Log.e("message", message.toString());
                    Toast.makeText(ApplyImageActivity.this,"图片申请成功！",Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });


    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_CANCELED) {

            switch (requestCode) {
                case IMAGE_REQUEST_CODE:
                    startPhotoZoom(data.getData());

                    break;
                case RESULT_REQUEST_CODE:
                    if (data != null) {
                        getImageToView(data);
                    }
                    break;
            }

        }

    }

    /**
     * 保存裁剪之后的图片数据
     *
     * @param data
     */
    @SuppressWarnings("deprecation")
    private void getImageToView(Intent data) {
        Bundle extras = data.getExtras();
        ByteArrayOutputStream stream = null;
        Bitmap photo = null;
        Drawable drawable = null;
        String picStr = null;
        byte[] bytes = null;
        if (extras != null) {
            switch (TAG) {
                case 3:
                    photo = extras.getParcelable("data");
                    drawable = new BitmapDrawable(photo);
                    ll_add1.setVisibility(View.GONE);
                    iv_img1.setVisibility(View.VISIBLE);
                    iv_img1.setImageDrawable(drawable);

                    stream = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);
                    bytes = stream.toByteArray();
                    // 将图片流以字符串形式存储下来
                    picStr = new String(Base64Coder.encodeLines(bytes));
                    photoStream[0] = picStr;
                    Log.e("photoStreamAAA", photoStream[0].toString());
                    break;
                case 4:
                    photo = extras.getParcelable("data");
                    drawable = new BitmapDrawable(photo);
                    ll_add2.setVisibility(View.GONE);
                    iv_img2.setVisibility(View.VISIBLE);
                    iv_img2.setImageDrawable(drawable);
                    stream = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);
                    bytes = stream.toByteArray();
                    // 将图片流以字符串形式存储下来
                    picStr = new String(Base64Coder.encodeLines(bytes));
                    photoStream[1] = picStr;
                    Log.e("photoStreamBBB", photoStream[1].toString());
                    break;
                case 5:

                    photo = extras.getParcelable("data");
                    drawable = new BitmapDrawable(photo);
                    ll_add3.setVisibility(View.GONE);
                    iv_img3.setVisibility(View.VISIBLE);
                    iv_img3.setImageDrawable(drawable);
                    stream = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);
                    bytes = stream.toByteArray();
                    // 将图片流以字符串形式存储下来
                    picStr = new String(Base64Coder.encodeLines(bytes));
                    photoStream[2] = picStr;
                    Log.e("photoStreamCCC", photoStream[0].toString());
                    break;
            }

        }

    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("return-data", true);
        intent.putExtra("scale", true); //黑边
        intent.putExtra("scaleUpIfNeeded", true) ;//黑边
        startActivityForResult(intent, RESULT_REQUEST_CODE);
    }

}
